// C Source File
// Created 11/19/2007; 7:30:50 PM

#include "Civilization.h"
#include "Interface.h"
#include "Terrain.h"
#include "Game.h"
#include "Constants.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// NewUnit ////////////////////////////////////////////////////////////////////////////////////////////////////////
// create a unit with given type and location /////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void NewUnit(char civ, char typeUnit, int x, int y, char grade) 
{
	if(civilization[civ].unitSize == UNIT_NUMBER) return;
	
	char count = civilization[civ].unitSize++;
	civilization[civ].unit[count].type = typeUnit;
	civilization[civ].unit[count].x = x;
	civilization[civ].unit[count].y = y;
	civilization[civ].unit[count].hpFULL = grade;
	civilization[civ].unit[count].hpCURR = grade;
	civilization[civ].unit[count].moveCURR = _units[typeUnit].moveMAX;
	civilization[civ].unit[count].settlerAction = -1;
	civilization[civ].unit[count].settlerActionTime = 0;
	civilization[civ].unit[count].isFortified = 0;
	civilization[civ].unit[count].xTarget = -1;
	civilization[civ].unit[count].yTarget = -1;
	
	RemoveFog(civ, x-1, y-1);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// RemoveUnit /////////////////////////////////////////////////////////////////////////////////////////////////////
// removes a unit from the array //////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void RemoveUnit(char index, char civ)
{
	char i;
	for(i = index; i < civilization[civ].unitSize-1; i++)
		civilization[civ].unit[i] = civilization[civ].unit[i+1];
	civilization[civ].unitSize--;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WriteUnit //////////////////////////////////////////////////////////////////////////////////////////////////////
// writes the stats of the units on the side screen ///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void WriteUnit(char unitIndex)
{
	char move[15], hp[15];
	
	sprintf(hp, "HP: %d/%d", civilization[0].unit[unitIndex].hpCURR, civilization[0].unit[unitIndex].hpFULL);
	sprintf(move, "Move: %d/%d", civilization[0].unit[unitIndex].moveCURR, _units[civilization[0].unit[unitIndex].type].moveMAX);
	
	DrawStr(111, 33, _units[civilization[0].unit[unitIndex].type].name, A_NORMAL);
	DrawStr(111, 43, hp,   A_NORMAL);
	DrawStr(111, 50, move, A_NORMAL);	
	
	if(IsSettler(civilization[0].unit[unitIndex]))
	{
		char action[20];
		int act = civilization[0].unit[unitIndex].settlerAction;
		
		if(act == -1) return;
		int t = Worldmap(civilization[0].unit[unitIndex].y-1, civilization[0].unit[unitIndex].x-1)-1;
		
		if(act == 10)
		{
			if(!HasRoad(civilization[0].unit[unitIndex].x-1, civilization[0].unit[unitIndex].y-1))
				act = 0;
			else if(terrain[t].irrigate && !HasIrrigation(civilization[0].unit[unitIndex].x-1, civilization[0].unit[unitIndex].y-1))
				act = 1;
			else if(terrain[t].mine && !HasMine(civilization[0].unit[unitIndex].x-1, civilization[0].unit[unitIndex].y-1))
				act = 2;
			//else if(!HasRailRoad(civilization[0].unit[unitIndex].x-1, civilization[0].unit[unitIndex].y-1))
			//	act = 3;
		}
		
		if(act == 0)
			sprintf(action, "Road: %d", terrain[t].movecost-civilization[0].unit[unitIndex].settlerActionTime);
		else if(act == 1)
			sprintf(action, "Irrg: %d", terrain[t].iTurns-civilization[0].unit[unitIndex].settlerActionTime);
		else if(act == 2)
			sprintf(action, "Mine: %d", terrain[t].mTurns-civilization[0].unit[unitIndex].settlerActionTime);
		//if(action == 3)
		//	sprintf(action, "RRoad: %d", terrain[t].movecost-civilization[0].unit[unitIndex].settlerActionTime);
		
		DrawStr(111, 57, action, A_NORMAL);	
	}
	else
	{
		char fight[15];
		sprintf(fight, "Atk: %d Def: %d", _units[civilization[0].unit[unitIndex].type].atk, _units[civilization[0].unit[unitIndex].type].def);
		DrawStr(111, 57, fight, A_NORMAL);	
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ActionUnit /////////////////////////////////////////////////////////////////////////////////////////////////////
// determines what a certain unit can do //////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ActionUnit(char num) 
{
	int y = 0, key = 0, end = 0;
	
	//menu strings
	char menuString[20][17];
	bool canBuildCity = 0;
	bool canBuildRoad = 0;
	bool canIrrigate = 0;;
	bool canMine = 0;
	
	if(civilization[0].unit[num].settlerAction != -1 && civilization[0].unit[num].settlerAction != 100)
	{
		DrawStr(111, 70, "Stop settler?", A_NORMAL);
		strcpy(menuString[0], "Yes");
		strcpy(menuString[1], "No");
		
		if(SideMenu(menuString, 2, 111, 80) == 0)
		{
			civilization[0].unit[num].settlerAction = -1;
			civilization[0].unit[num].settlerActionTime = 0;
			civilization[0].unit[num].xTarget = -1;
			civilization[0].unit[num].yTarget = -1;
		}
		return;
	}
	
	if(civilization[0].unit[num].moveCURR == 0)
	{
		if(civilization[0].unit[num].xTarget != -1 || civilization[0].unit[num].settlerAction == 100)
		{
			DrawStr(111, 70, "Stop moving?", A_NORMAL);
			strcpy(menuString[0], "Yes");
			strcpy(menuString[1], "No");
			
			if(SideMenu(menuString, 2, 111, 80) == 0)
			{
				civilization[0].unit[num].xTarget = -1;
				civilization[0].unit[num].yTarget = -1;
				civilization[0].unit[num].settlerAction = -1;
			}
		}
		else
		{
			DrawStr(111, 70, "No moves", A_NORMAL);
			ngetchx();
		}
		return;
	}
	
	//copy over strings
	strcpy(menuString[end++], "Move");
	if (civilization[0].unit[num].isFortified) strcpy(menuString[end++], "Unfortify");
	else																			 strcpy(menuString[end++], "Fortify");
	strcpy(menuString[end++], "Explore");
	strcpy(menuString[end++], "Disband");
	
	if (IsSettler(civilization[0].unit[num]))
	{
		strcpy(menuString[end++], "Automate");
		if((canBuildCity = CanBuildCity(num, 0))) strcpy(menuString[end++], "Build City");
		if((canBuildRoad = CanBuildRoad(num, 0))) strcpy(menuString[end++], "Build Road");
		if((canIrrigate = CanIrrigate(num, 0)))   strcpy(menuString[end++], "Irrigate");
		if((canMine = CanMine(num, 0)))           strcpy(menuString[end++], "Mine");
	}
	
	char i = SideMenu(menuString, end, 111, 70);
	
	if(i != -1)
	{
		if(i == 0)
		{
			MoveUnitCursor(num);
			DrawScreen();
		}
		else if(i == 1)
		{
			civilization[0].unit[num].isFortified = 1 - civilization[0].unit[num].isFortified;	
		}
		else if(i == 2)
		{
			civilization[0].unit[num].settlerAction = 100;
			while(Explore(0, num) && civilization[0].unit[num].moveCURR > 0) ;
			if(civilization[0].unit[num].moveCURR > 0) civilization[0].unit[num].settlerAction = -1;
			DrawScreen();
		}
		else if(i == 3)
		{
			if(Confirm())
			{
				RemoveUnit(num, 0);
				DrawScreen();
			}
		}
		else if(i == 4)
		{
			civilization[0].unit[num].settlerAction = 10;
			AutomateSettler(0, num);
			if(civilization[0].unit[num].settlerAction != -1)
			{
				//DebugStr("AutomatedSettler Worked!");
				int x[30], y[30];
				int dist = getPath(civilization[0].unit[num].x-1,     civilization[0].unit[num].y-1, 
										       civilization[0].unit[num].xTarget, civilization[0].unit[num].yTarget, 
										       x, y, 0);
				MoveUnit(0, num, x, y, dist);
				DrawScreen();
			}
		}
		else
		{
			SettlerAction(num, 0, canBuildCity, canBuildRoad, canIrrigate, canMine, i - 5);
		}
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MoveUnit ///////////////////////////////////////////////////////////////////////////////////////////////////////
// moves the unit along the target path ///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MoveUnit(char civ, char unit, int x[30], int y[30], int dist)
{
	while(dist > 0 && civilization[civ].unit[unit].moveCURR > 0)
	{
		// decrease variable
		dist--;
		
		// make sure that next position is viable
		int mapSq = Worldmap(y[dist], x[dist])-1;
		if(_units[civilization[0].unit[unit].type].area == 1 && mapSq == WATER) break;
		else if(_units[civilization[0].unit[unit].type].area == 2 && mapSq != WATER && !IsCityAt(civ, x[dist], y[dist])) break;
		
		// set new position
		civilization[civ].unit[unit].x = x[dist]+1;
		civilization[civ].unit[unit].y = y[dist]+1;
		
		// remove the fog
		RemoveFog(civ, x[dist], y[dist]);
		
		// decrease movement counter
		civilization[civ].unit[unit].moveCURR--;
	}
	
	// not fortified since it moved
	civilization[civ].unit[unit].isFortified = 0;
	
	// if reached target
	if(dist < 1)
	{
		if(civilization[civ].unit[unit].settlerAction != 10)
		{
			civilization[civ].unit[unit].xTarget = -1;
			civilization[civ].unit[unit].yTarget = -1;
		}
	}
	else if(civilization[civ].unit[unit].moveCURR > 0)
	{
		dist = getPath(civilization[civ].unit[unit].x-1,     civilization[civ].unit[unit].y-1, 
									 civilization[civ].unit[unit].xTarget, civilization[civ].unit[unit].yTarget, 
									 x, y, civ);
		if(dist > 0)
		{
			MoveUnit(civ, unit, x, y, dist);
		}
		else
		{
			if(civilization[civ].unit[unit].settlerAction != 10)
			{
				civilization[civ].unit[unit].xTarget = -1;
				civilization[civ].unit[unit].yTarget = -1;
			}
		}
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GetUnitIndicesCursor ///////////////////////////////////////////////////////////////////////////////////////////
// it searched for units at the cursor & populates an array ///////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
char GetUnitIndicesCursor(char *units)
{
	return GetUnitIndicesAt(units, GetCursorX(), GetCursorY());
}
char GetUnitIndicesAt(char *units, int x, int y)
{
	char loop, count = 0;
	
	for(loop = 0; loop < civilization[0].unitSize; loop++)
		if (civilization[0].unit[loop].x == x && 
		    civilization[0].unit[loop].y == y)
			units[count++] = loop;
	
	units[count] = -1;
	return count;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GetNumberOfUnitsCursor /////////////////////////////////////////////////////////////////////////////////////////
// it finds all the units at the point and counts them ////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
char GetNumberOfUnitsAt(char civ, int x, int y)
{
	char loop, count = 0;
	
	for(loop = 0; loop < civilization[civ].unitSize; loop++)
		if (civilization[civ].unit[loop].x == x && 
		    civilization[civ].unit[loop].y == y)
			count++;
	
	return count;
}
char GetNumberOfUnitsCursor(char civ)
{
	return GetNumberOfUnitsAt(civ, GetCursorX(), GetCursorY());
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// SettlerAction //////////////////////////////////////////////////////////////////////////////////////////////////
// makes a settler do something ///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void SettlerAction(char num, char civ, bool canBuildCity, bool canBuildRoad, bool canIrrigate, bool canMine, bool i)
{
	if(i == 0)
	{
		if(canBuildCity) { BuildCity(num, civ); DrawScreen(); }
		else if(canBuildRoad) civilization[civ].unit[num].settlerAction = 0;//road
		else if(canIrrigate) civilization[civ].unit[num].settlerAction = 1;//irrigation
		else if(canMine) civilization[civ].unit[num].settlerAction = 2;//mine
	}
	else if(i == 1)
	{
		if(canBuildCity)
		{
			if(canBuildRoad) civilization[civ].unit[num].settlerAction = 0;//road
			else if(canIrrigate) civilization[civ].unit[num].settlerAction = 1;//irrigation
			else if(canMine) civilization[civ].unit[num].settlerAction = 2;//mine
		}
		else if(canBuildRoad)
		{
			if(canIrrigate) civilization[civ].unit[num].settlerAction = 1;//irrigation
			else if(canMine) civilization[civ].unit[num].settlerAction = 2;//mine
		}
		else if(canIrrigate)
		{
			if(canMine) civilization[civ].unit[num].settlerAction = 2;//mine
		}
		else if(canMine)
		{
			;//???
		}
	}
	else if(i == 2)
	{
		if(canBuildCity)
		{
			if(canBuildRoad)
			{
				if(canIrrigate) civilization[civ].unit[num].settlerAction = 1;//irrigation
				else if(canMine) civilization[civ].unit[num].settlerAction = 2;//mine
			}
			else if(canIrrigate)
			{
				if(canMine) civilization[civ].unit[num].settlerAction = 2;//mine
			}
			else if(canMine)
			{
				;//???
			}
		}
		else if(canBuildRoad)
		{
			if(canIrrigate)
			{
				if(canMine) civilization[civ].unit[num].settlerAction = 2;//mine
			}
			else if(canMine)
			{
				;//???
			}
		}
		else if(canIrrigate)
		{
			if(canMine)
			{
				;//???
			}
		}
		else if(canMine)
		{
			;//???
		}
	}
	else if(i == 3)
	{
		if(canBuildCity)
		{
			if(canBuildRoad)
			{
				if(canIrrigate)
				{
					if(canMine) civilization[civ].unit[num].settlerAction = 2;//mine
				}
			}
			else if(canIrrigate)
			{
				;//???
			}
			else if(canMine)
			{
				;//???
			}
		}
		else if(canBuildRoad)
		{
			if(canIrrigate)
			{
				;//???
			}
			else if(canMine)
			{
				;//???
			}
		}
		else if(canIrrigate)
		{
			if(canMine)
			{
				;//???
			}
		}
		else if(canMine)
		{
			;//???
		}
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Can... /////////////////////////////////////////////////////////////////////////////////////////////////////////
// determines what a settler can do ///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//maybe check to be not on water???
bool CanBuildCity(char index, char civ)
{
	char i, j;
	
	for(i = 0; i < civNumb; i++)
	for(j = 0; j < civilization[i].citySize; j++)
	{		
		if ((civilization[i].city[j].x < civilization[civ].unit[index].x + 2 && civilization[i].city[j].x > civilization[civ].unit[index].x - 2) &&
		    (civilization[i].city[j].y < civilization[civ].unit[index].y + 2 && civilization[i].city[j].y > civilization[civ].unit[index].y - 2))
    {
			return 0;
    }
	}
	
	return 1;
}
bool CanBuildRoad(char index, char civ)
{
	int x = civilization[civ].unit[index].x;
	int y = civilization[civ].unit[index].y;
	
	if(HasRoad(x-1,y-1)) return 0;
	
	if(IsCityAt(civ, x, y)) return 0;
	
	return 1;
}
bool CanIrrigate(char index, char civ)
{
	int x = civilization[civ].unit[index].x;
	int y = civilization[civ].unit[index].y;
	
	if(HasIrrigation(x-1,y-1)) return 0;
	
	if(!terrain[Worldmap(y-1, x-1)-1].irrigate) return 0;
	
	if(IsCityAt(civ, x, y)) return 0;
	
	return 1;
}
bool CanMine(char index, char civ)
{
	int x = civilization[civ].unit[index].x;
	int y = civilization[civ].unit[index].y;
	
	if(HasMine(x-1,y-1)) return 0;
	
	if(!terrain[Worldmap(y-1, x-1)-1].mine) return 0;
	
	if(IsCityAt(civ, x, y)) return 0;
	
	return 1;
}

// helper function for Explore
bool FindFog(char civ, int x, int y)
{
	if(x < 0 || y < 0 || x >= game.mapWidth || y >= game.mapHeight) return 0;
	return !GetBit(civilization[civ].fog, x + y * game.mapWidth);
}

bool ExploreTo(char civ, char unit, int x, int y)
{
	civilization[civ].unit[unit].xTarget = x;
	civilization[civ].unit[unit].yTarget = y;
	
	int _x[30], _y[30];
	int dist = _getPath(civilization[civ].unit[unit].x-1, civilization[civ].unit[unit].y-1, x, y, _x, _y, civ, 5);
	if(dist < 1) return 0;
	MoveUnit(civ, unit, _x, _y, dist);
	return 1;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Explore ////////////////////////////////////////////////////////////////////////////////////////////////////////
// the unit will explore the map //////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Explore(char civ, char unit)
{
	int X = civilization[civ].unit[unit].x - 1;
	int Y = civilization[civ].unit[unit].y - 1;
	
	// if already had a target from last time, keep going
	if(civilization[civ].unit[unit].xTarget != -1)
		ExploreTo(civ, unit, civilization[civ].unit[unit].xTarget, civilization[civ].unit[unit].yTarget);
	
	// search random direction for fog, then try to go there (max 5 steps)
	int layer = 2;
	while(layer < 4)
	{
		int i;
		switch(random(4))
		{
		case 0:
			for(i = layer; i > -layer; i--) if(FindFog(civ, X-layer, Y+i) && ExploreTo(civ, unit, X-layer, Y+i)) return 1;
			for(i = -layer; i < layer; i++) if(FindFog(civ, X+layer, Y+i) && ExploreTo(civ, unit, X+layer, Y+i)) return 1;
			for(i = -layer; i < layer; i++) if(FindFog(civ, X+i, Y-layer) && ExploreTo(civ, unit, X+i, Y-layer)) return 1;
			for(i = layer; i > -layer; i--) if(FindFog(civ, X+i, Y-layer) && ExploreTo(civ, unit, X+i, Y-layer)) return 1;
			break;
		case 1:
			for(i = -layer; i < layer; i++) if(FindFog(civ, X+layer, Y+i) && ExploreTo(civ, unit, X+layer, Y+i)) return 1;
			for(i = -layer; i < layer; i++) if(FindFog(civ, X+i, Y-layer) && ExploreTo(civ, unit, X+i, Y-layer)) return 1;
			for(i = layer; i > -layer; i--) if(FindFog(civ, X+i, Y-layer) && ExploreTo(civ, unit, X+i, Y-layer)) return 1;
			for(i = layer; i > -layer; i--) if(FindFog(civ, X-layer, Y+i) && ExploreTo(civ, unit, X-layer, Y+i)) return 1;
			break;
		case 2:
			for(i = -layer; i < layer; i++) if(FindFog(civ, X+i, Y-layer) && ExploreTo(civ, unit, X+i, Y-layer)) return 1;
			for(i = layer; i > -layer; i--) if(FindFog(civ, X+i, Y-layer) && ExploreTo(civ, unit, X+i, Y-layer)) return 1;
			for(i = layer; i > -layer; i--) if(FindFog(civ, X-layer, Y+i) && ExploreTo(civ, unit, X-layer, Y+i)) return 1;
			for(i = -layer; i < layer; i++) if(FindFog(civ, X+layer, Y+i) && ExploreTo(civ, unit, X+layer, Y+i)) return 1;
			break;
		default:
			for(i = layer; i > -layer; i--) if(FindFog(civ, X+i, Y-layer) && ExploreTo(civ, unit, X+i, Y-layer)) return 1;
			for(i = layer; i > -layer; i--) if(FindFog(civ, X-layer, Y+i) && ExploreTo(civ, unit, X-layer, Y+i)) return 1;
			for(i = -layer; i < layer; i++) if(FindFog(civ, X+layer, Y+i) && ExploreTo(civ, unit, X+layer, Y+i)) return 1;
			for(i = -layer; i < layer; i++) if(FindFog(civ, X+i, Y-layer) && ExploreTo(civ, unit, X+i, Y-layer)) return 1;
			break;
		}
		
		layer++;
	}
	
	return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AutomateSettler ////////////////////////////////////////////////////////////////////////////////////////////////
// directs a settler to automatically improve the surrounding terrain /////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void AutomateSettler(char civ, char unit)
{
	//DebugStr("Start AS");
	// 1 - find something good to improve
	// 2 - move towards that
	// 3 - build roads on the way (build road if it doesnt have one)
	int x = civilization[civ].unit[unit].x - 1;
	int y = civilization[civ].unit[unit].y - 1;
	char val = -10;
	
	char i, j;
	char iBest = 0, jBest = 0;
	for(i = -3; i < 4; i++)
	for(j = -3; j < 4; j++)
	{
		int _x = x + i;
		int _y = y + j;
		
		if(_x < 0 || _y < 0 || _x >= game.mapWidth || _y >= game.mapHeight) continue;
		if(Worldmap(_y, _x)-1 == WATER) continue;
		if(!GetBit(civilization[civ].fog, _x + _y * game.mapWidth)) continue;
		
		char dist = max(abs(i), abs(j));
		char _val = 0;
		
		if(terrain[Worldmap(_y, _x)-1].irrigate && !HasIrrigation(x,y))
			_val += terrain[Worldmap(_y, _x)-1].iAI;
		
		if(terrain[Worldmap(_y, _x)-1].mine && !HasMine(x,y))
			_val += terrain[Worldmap(_y, _x)-1].mAI;
	
		_val -= dist;
		
		if(_val > val)
		{
			val = _val;
			iBest = i;
			jBest = j;
		}
	}
	
	// couldn't find anything good
	if(val == -10)
	{
		civilization[civ].unit[unit].settlerAction = -1;
		civilization[civ].unit[unit].xTarget = -1;
		civilization[civ].unit[unit].yTarget = -1;
		//DebugStr("Bad End AS");
		return;
	}
	
	civilization[civ].unit[unit].xTarget = x + iBest;
	civilization[civ].unit[unit].yTarget = y + jBest;
	//DebugStr("Good End AS");
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MoveRandom /////////////////////////////////////////////////////////////////////////////////////////////////////
// moves the unit in a random direction ///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MoveRandom(char civ, char unit)
{
	int x = civilization[civ].unit[unit].x-1;
	int y = civilization[civ].unit[unit].y-1;
	
	if(y > 0) if(Worldmap(y-1,x)-1 != WATER) { civilization[civ].unit[unit].y = (y-1)+1; civilization[civ].unit[unit].moveCURR--; return; }
	if(y+1 < game.mapHeight) if(Worldmap(y+1,x)-1 != WATER) { civilization[civ].unit[unit].y = (y+1)+1; civilization[civ].unit[unit].moveCURR--; return; }
	
	if(x > 0)
	{
		if(Worldmap(y,x-1)-1 != WATER) { civilization[civ].unit[unit].x = (x-1)+1; civilization[civ].unit[unit].moveCURR--; return; }
		if(y > 0) if(Worldmap(y-1,x-1)-1 != WATER) { civilization[civ].unit[unit].x = (x-1)+1; civilization[civ].unit[unit].y = (y-1)+1; civilization[civ].unit[unit].moveCURR--; return; }
		if(y+1 < game.mapHeight) if(Worldmap(y+1,x-1)-1 != WATER) { civilization[civ].unit[unit].x = (x-1)+1; civilization[civ].unit[unit].y = (y+1)+1; civilization[civ].unit[unit].moveCURR--; return; }
	}
	
	if(x+1 < game.mapWidth)
	{
		if(Worldmap(y,x+1)-1 != WATER) { civilization[civ].unit[unit].x = (x+1)+1; civilization[civ].unit[unit].moveCURR--; return; }
		if(y > 0) if(Worldmap(y-1,x+1)-1 != WATER) { civilization[civ].unit[unit].x = (x+1)+1; civilization[civ].unit[unit].y = (y-1)+1; civilization[civ].unit[unit].moveCURR--; return; }
		if(y+1 < game.mapHeight) if(Worldmap(y+1,x+1)-1 != WATER) { civilization[civ].unit[unit].x = (x+1)+1; civilization[civ].unit[unit].y = (y+1)+1; civilization[civ].unit[unit].moveCURR--; return; }
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// aiDoUnits //////////////////////////////////////////////////////////////////////////////////////////////////////
// provides the ai control of units ///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void AI_DoUnits(char civ)
{
	Clr(21, 61, 139, 69);
	DrawStr(70, 63, "Units", A_NORMAL);
	int x = 20;
	
	char i;
	for(i = 0; i < civilization[civ].unitSize; i++)
	{
		if(civilization[civ].unit[i].moveCURR != 0)
		{
			if(IsSettler(civilization[civ].unit[i]))
			{
				if(civilization[civ].citySize == 0)
				{
					BuildCity(i, civ);
					AI_UpdateCityProduction(civ, civilization[civ].citySize-1);
				}
				else
				{
					// 3:1 ratio for cities to settlers
					if(civilization[civ].citySize / 3 > aiCivs[civ-1].autoSettlers)
					{
						// try automating
						civilization[civ].unit[i].settlerAction = 10;
						AutomateSettler(civ, i);
						
						// if it worked
						if(civilization[civ].unit[i].settlerAction != -1)
						{
							int x[30], y[30];
							int dist = getPath(civilization[civ].unit[i].x-1,     civilization[civ].unit[i].y-1, 
													       civilization[civ].unit[i].xTarget, civilization[civ].unit[i].yTarget, 
													       x, y, civ);
							MoveUnit(civ, i, x, y, dist);
							aiCivs[civ-1].autoSettlers++;
						}
						else MoveRandom(civ, i);
					}
					else
					{
						// find a place to build a new city
					}
				}
			}
			else
			{
				civilization[civ].unit[i].settlerAction = 100;
				while(civilization[civ].unit[i].moveCURR > 0) 
					if(!Explore(civ, i)) 
					{
						civilization[civ].unit[i].settlerAction = -1;
						break;
					}
			}
		}
		
		// increase progress bar percent
		int _x = (i+1) * 120 / civilization[civ].unitSize + 20;
		while(x < _x - 1)
			DrawLine(++x, 61, x, 69, A_XOR);
	}
}
